package com.xcity.game.combat.skill.impl;

import java.util.List;

import com.xcity.game.combat.Action;
import com.xcity.game.combat.ActionEffect;
import com.xcity.game.combat.CombatUtils;
import com.xcity.game.combat.Round;
import com.xcity.game.combat.attr.CombatAttributeType;
import com.xcity.game.combat.buff.CombatBuff;
import com.xcity.game.combat.skill.CombatSkill;
import com.xcity.game.combat.unit.CombatUnit;
import com.xcity.game.common.GameStaticConfig;
import com.xcity.game.skill.Skill;

/**
 * 突飞猛进<br/>
 * 高速穿过战场，对敌方所有目标造成{0=80#0}%技能伤害，若暴击自身速度增加{1=50#0}%
 * @author yang.li
 *
 */
public class COMBAT_SKILL_90105 extends CombatSkill {

	public COMBAT_SKILL_90105(Skill skill) {
		super(skill);
	}

	@Override
	public void calcDamage(Action action) {
		float rate = skill.getParameter(0) / GameStaticConfig.percentBase;
		List<CombatUnit> targets = action.target.all();
		boolean crit = false;
		for (int i = 0; i < targets.size(); i++) {
			CombatUnit target = targets.get(i);
			if (!target.isDied()) {
				ActionEffect effect = action.addTarget(target);
				target = effect.getUnit(); // 修正目标
				CombatUtils.calcMagicAttack(effect, rate);
				if (effect.getResult() == ActionEffect.RESULT_CRIT) {
					crit = true;
				}
			}
		}
		if (crit) {
			CombatUnit source = action.unit;
			action.addBuff(new BUFF(skill, source));
		}
	}
	
	static class BUFF extends CombatBuff {
		int oldAttackSpeed;
		public BUFF(Skill skill, CombatUnit owner) {
			super(skill.getTemplate().getCombatBuff(), owner);
			active = true;
			int speed = owner.getAttackSpeed();
			oldAttackSpeed = speed;
			speed += speed * skill.getParameter(1) / GameStaticConfig.percentBase;
			owner.setAttributeValue(CombatAttributeType.ATTACK_SPEED, speed);
		}
		@Override
		public void roundEnd(Round round) {
			super.roundEnd(round);
			if (state == STATE_REMOVED) {
				owner.setAttributeValue(CombatAttributeType.ATTACK_SPEED, oldAttackSpeed);
			}
		}
	}

}
